package com.briup.jz.service.impl;

import com.briup.jz.bean.OrderComment;
import com.briup.jz.bean.OrderCommentExample;
import com.briup.jz.dao.OrderCommentMapper;
import com.briup.jz.service.IOrderCommentService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderCommentImpl implements IOrderCommentService {
    @Autowired
    private OrderCommentMapper orderCommentMapper;
    @Override
    public void saveOrUpdate(OrderComment orderComment) throws CustomerException {
		//        判断订单id是否存在
		//        如果id 不为空则插入信息
        if (orderComment.getId()==null){
        	orderCommentMapper.insert(orderComment);
        }else {

            OrderComment orderComment1=orderCommentMapper.selectByPrimaryKey(orderComment.getId());
            if (orderComment1==null){
               throw new CustomerException("修改的订单评论不存在");
            }else{
            	//如果能查到则修改评论
                orderCommentMapper.updateByPrimaryKey(orderComment);
            }
        }
    }

    @Override
    public void deleteByid(Long id) throws CustomerException {
OrderComment orderComment=orderCommentMapper.selectByPrimaryKey(id);
//根据主键查找评论信息
if (orderComment==null){//如果评论为空
    throw  new CustomerException("你删除的评论不存在");
}
orderCommentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<OrderComment> query(Long id) {
        OrderCommentExample example =new OrderCommentExample();
        //判断用户id是否为空
        if (id!=null){
//            如果不为空则根据顾客id 查询,否则则查询所有
            example.createCriteria().andOrderIdEqualTo(id);
        }
        return orderCommentMapper.selectByExample(example);
    }
}
